package by.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import by.grsu.hd.exception.IllegalInternalStateException;
//import by.servlet.bean.MediaPlayer;
//import by.servlet.bean.IMediaPlayer;

import by.grsu.hd.devices.MediaPlayer;
import by.grsu.hd.model.IMediaPlayer;

/**
 * Servlet implementation class Servlet
 */
@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private String[] str;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Servlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		IMediaPlayer iMediaPlayer = new MediaPlayer();

		response.setContentType("text/html");

		PrintWriter out = response.getWriter();

		out.println("<html>");
		out.println("<head>");
		out.println("<title> First Servlet Example</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("<h3>My Media Player Example</h3>");
		out.println("Parameters in this request:<br>");

		String radioB = request.getParameter("track");
		String butt = request.getParameter("button");
		String[] str = iMediaPlayer.getAFiles();
		String status = null;
		out.println("<br>");

		// List<MediaPlayer> files = new ArrayList<MediaPlayer>();

		//

		// System.out.println("w");
		out.println("<form action=\"Servlet\" method=POST>");

		out.println("<table>");
		out.println("<tr>");
		out.println("<td>");
		// out.println(""+butt);
		// System.out.println(butt);
		
		if (butt != null) {
			
			try {
				if (butt.equals("play") && radioB != null) {
					iMediaPlayer.setControl(1, "Play");
					status = iMediaPlayer.getControl();
					// System.out.println(status);
					out.println("<h3>Status: " + status + "</h3>");
					if (radioB != null){
						out.println("Now pleying: " + radioB);
						out.println("<IMG SRC=images/LightMusic.gif BORDER=0 WIDTH=200 HEIGHT=40>");
					}
					else
						out.println("Now pleying: " + "");
				} else if (butt.equals("pause")) {
					iMediaPlayer.setControl(2, "Pause");
					out.println("<h3>Status: " + iMediaPlayer.getControl()
							+ "</h3>");
					
					
					
				} else if (butt.equals("stop")) {
					iMediaPlayer.setControl(3, "Stop");
					out.println("<h3>Status: " + iMediaPlayer.getControl()
							+ "</h3>");
				} else {
					out.println("<h3>Status: file not found </h3>");
				}
			} catch (IllegalInternalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		out.println("</td>");
		out.println("</tr>");
		out.println("<tr>");
		out.println("<td>");
		for (int i = 0; i < str.length; i++) {
			out.println("<LI>");
			out.println("<INPUT TYPE=radio NAME=track VALUE=" + str[i] + ">"
					+ str[i]);

			// System.out.println("q");
			// System.out.println(str[i]);
		}
		out.println("</td>");
		out.println("<td>");

		// if (radio. )

		out.println("</td>");
		out.println("</tr>");
		out.println("<tr>");
		out.println("<td>");

		out.print("<input type=submit name=button value=play>");
		out.print("<input type=submit name=button value=pause>");
		out.print("<input type=submit name=button value=stop>");
		out.println("</td>");
		out.println("</tr>");
		out.println("</table>");
		out.print("</form>");

		out.println("</body>");
		out.println("</html>");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
